package com.dhcc.cdc.busi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcBsEmrStructMapper;
import com.dhcc.cdc.busi.entity.CdcBsEmrStructEntity;
import com.dhcc.cdc.busi.entity.CdcEmrEntity;
import com.dhcc.cdc.busi.service.ICdcBsEmrStructService;
import com.dhcc.cdc.util.DHCAI;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * 电子病历语义分析解析后结果表数据服务层接口
 * 
 * @ClassName: CdcBsEmrStructServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-03-16 10:26:09
 */
@Service("cdcBsEmrStructService")
public class CdcBsEmrStructServiceImpl extends BaseServiceImpl<CdcBsEmrStructMapper, CdcBsEmrStructEntity> implements ICdcBsEmrStructService {

	@Autowired
	private CdcBsEmrStructMapper cdcBsEmrStructMapper;

	@Override
	public List<CdcBsEmrStructEntity> page(Page<CdcBsEmrStructEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcBsEmrStructEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public void emrStructByNLP(String dhcAiUrl, List<CdcEmrEntity> emrRecordList) {
		//获取电子病历结构化信息
		JSONArray emrArray = new JSONArray();
		//Map<String, Object> emrMap = new HashMap<String, Object>();
		// 开启多线程查询科室
		List<Future> list = new ArrayList<Future>();
		ExecutorService emrPool = Executors.newFixedThreadPool(5);
		long nowTime = new Date().getTime();
		for(CdcEmrEntity emr : emrRecordList){

			//查询该电子病历是否已被解析存储 并且更新日期为前天
			int count = cdcBsEmrStructMapper.selectCount(new EntityWrapper<CdcBsEmrStructEntity>().eq("emr_id", emr.getId()));

			Date updateDate = emr.getUpdateDate();
			//如果更新日期为空 默认为2天前
			if(updateDate==null){
				Calendar canlendar = Calendar.getInstance(); // java.util包
				canlendar.add(Calendar.DATE, -2); // 日期减 如果不够减会将月变动
				updateDate = canlendar.getTime();
			}
			long updateTime = updateDate.getTime();
			long isTime = nowTime - updateTime;
			if(count==0 || isTime<=(24*60*60*1000)){//未被解析或者更新日期小于一天
				String content = emr.getContent();
				if(!CommonUtil.isEmpty(content)){
					DHCAICallable c = new DHCAICallable(dhcAiUrl,emr.getTitle(),content,emr.getUpdateDate(),emr.getSubTitle(),"");
					Future f = emrPool.submit(c);
					list.add(f);
					try {
						JSONArray obj = (JSONArray) f.get();
						String docId = emr.getId().toString();
						if(count!=0){
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							Wrapper wpr = new EntityWrapper<CdcBsEmrStructEntity>().eq("emr_id", docId);
							cdcBsEmrStructMapper.updateForSet("emr_nlp_result='"+obj.toJSONString()+"',emr_update_date='"+sdf.format(emr.getUpdateDate())+"'", wpr);
						}else{
							CdcBsEmrStructEntity emrStructEntity = new CdcBsEmrStructEntity();
							emrStructEntity.setEmrId(docId);
							emrStructEntity.setEmrNlpResult(obj.toJSONString());
							emrStructEntity.setEmrTitle(emr.getTitle());
							emrStructEntity.setEmrSubTitle(emr.getSubTitle());
							emrStructEntity.setEmrDate(emr.getEmrDate());
							emrStructEntity.setEmrUpdateDate(emr.getUpdateDate());
							emrStructEntity.setEpisodeId(emr.getEpisodeId());
							cdcBsEmrStructMapper.insert(emrStructEntity);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				}
			}
		}
		emrPool.shutdown();
	}

	/**
	 *
	 * @ClassName: DHCAICallable
	 * @Description ：   调用东华语义分析多线程
	 * @Date: 2019年8月1日 下午2:34:08
	 * @Author WuJiaFeng
	 * @Version 1.0 DHC-VTE
	 */
	private class DHCAICallable implements Callable<Object> {
		private String title;
		private String content;
		private String dhcai_url;
		private Date date;
		private String subTitle;
		private String examDesc;

		public DHCAICallable(String dhcai_url,String title,String content,Date date,String subTitle,String examDesc) {
			this.dhcai_url = dhcai_url;
			this.title = title;
			this.content = content;
			this.date = date;
			this.subTitle = subTitle;
			this.examDesc = examDesc;
		}

		@Override
		public Object call() throws Exception {
			JSONArray resultArr = DHCAI.parse(dhcai_url,content);
			return resultArr;
		}
	}
}